﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using System.IO;

namespace Shop.Infrastructure.Web
{
    /// <summary>
    /// 实现多模版加载
    /// </summary>
    public class ShopRazorViewEngine : RazorViewEngine
    {
        public string DefaultViewTemplate = "Classic";
        public ShopRazorViewEngine(string defaultViewTemplate = "Classic")
            : this(null, defaultViewTemplate)
        {
        }

        public ShopRazorViewEngine(IViewPageActivator viewPageActivator, string defaultViewTemplate = "Classic")
            : base(viewPageActivator)
        {
            this.DefaultViewTemplate = defaultViewTemplate;
            this.AddtionDefaultMutilView(this.AreaViewLocationFormats);
            this.AddtionDefaultMutilView(this.AreaMasterLocationFormats);
            this.AddtionDefaultMutilView(this.AreaPartialViewLocationFormats);
            this.AddtionDefaultMutilView(this.ViewLocationFormats);
            this.AddtionDefaultMutilView(this.MasterLocationFormats);
            this.AddtionDefaultMutilView(this.PartialViewLocationFormats);
        }

        private void AddtionDefaultMutilView(string[] viewLoacationFormats)
        {
            string viewTemplateStr = string.Format("/Views/{0}/", this.DefaultViewTemplate);
            for (int i = 0; i < viewLoacationFormats.Length; i++)
            {
                string locationFormat = viewLoacationFormats[i];
                viewLoacationFormats[i] = locationFormat.Replace("/Views/", viewTemplateStr);
            }
        }
    }
}

